import { pinia } from "@/pinia";
import { DeviceEnum, SIDEBAR_CLOSED, SIDEBAR_OPENED } from "@@/constants/app-key";
import { getSidebarStatus, setSidebarStatus } from "@@/utils/cache/local-storage";

/** 设置侧边栏状态本地缓存 */
function handleSidebarStatus(opened) {
  opened ? setSidebarStatus(SIDEBAR_OPENED) : setSidebarStatus(SIDEBAR_CLOSED);
}

export const useAppStore = defineStore("app", () => {
  // 侧边栏状态
  const sidebar = reactive({
    opened: getSidebarStatus() !== SIDEBAR_CLOSED,
    withoutAnimation: false
  });

  // 设备类型
  const device = ref(DeviceEnum.Desktop);

  // 监听侧边栏 opened 状态
  watch(
    () => sidebar.opened,
    (opened) => {
      handleSidebarStatus(opened);
    }
  );

  // 切换侧边栏
  const toggleSidebar = (withoutAnimation) => {
    sidebar.opened = !sidebar.opened;
    sidebar.withoutAnimation = withoutAnimation;
  };

  // 关闭侧边栏
  const closeSidebar = (withoutAnimation) => {
    sidebar.opened = false;
    sidebar.withoutAnimation = withoutAnimation;
  };

  // 切换设备类型
  const toggleDevice = (value) => {
    device.value = value;
  };

  return { device, sidebar, toggleSidebar, closeSidebar, toggleDevice };
});

/**
 * @description 在 SPA 应用中可用于在 pinia 实例被激活前使用 store
 * @description 在 SSR 应用中可用于在 setup 外使用 store
 */
export function useAppStoreOutside() {
  return useAppStore(pinia);
}
